//
//  GTVistorView.swift
//  swiftWEIbo
//
//  Created by mac－gt on 15/11/23.
//  Copyright © 2015年 mac－gt. All rights reserved.
//

import UIKit

protocol GTVistorViewDelegate: NSObjectProtocol{
    func vistorViewRegister()

    func vistorViewLogin()
}

class GTVistorView: UIView {
    weak var vistorViewDelegate: GTVistorViewDelegate?
   override init(frame: CGRect) {
        super.init(frame:frame)
        setUP()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    func setUP(){
        addSubview(iconView)
        addSubview(coverView)
        addSubview(imageView)
        addSubview(descLable)
        addSubview(regirstBtn)
        addSubview(loginBtn)
        iconView.translatesAutoresizingMaskIntoConstraints=false
        imageView.translatesAutoresizingMaskIntoConstraints=false
        descLable.translatesAutoresizingMaskIntoConstraints=false
        regirstBtn.translatesAutoresizingMaskIntoConstraints=false
        loginBtn.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
         addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: descLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: descLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint( NSLayoutConstraint(item: descLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: regirstBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: descLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: regirstBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: regirstBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        addConstraint(NSLayoutConstraint(item: regirstBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: descLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: regirstBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    func tranAnimation(){
        let ani=CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue=2 * M_PI
        ani.duration=20
        ani.removedOnCompletion=false
        ani.repeatCount=MAXFLOAT
        iconView.layer.addAnimation(ani, forKey: "ani")
    }
    func pauseAnimation(){
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        iconView.layer.speed = 0
    
        iconView.layer.timeOffset = pauseTime
    }
    

    func resumeAnimation() {
        let pauseTime = iconView.layer.timeOffset
        
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
       
    }
 
    func changeViewImage(imageName: String, lableContent: String){
        iconView.image=UIImage(named: imageName)
        descLable.text=lableContent
        imageView.hidden=true
        coverView.hidden=true
    }
    func registBtnClick(){
        vistorViewDelegate?.vistorViewRegister()
    }
    func loginBtnClick(){
        vistorViewDelegate?.vistorViewLogin()
    }
    private lazy var coverView: UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconView: UIImageView={
        let view1=UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return view1
    }()
    private lazy var imageView: UIImageView={
        let view1=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return view1
    }()
    private lazy var descLable: UILabel={
       let lable=UILabel()
        lable.text="新浪微博能够时刻关注你想关注的人"
        lable.font=UIFont.systemFontOfSize(14)
        lable.textColor=UIColor.darkGrayColor()
        lable.numberOfLines=0
        lable.sizeToFit()
        return lable
    }()
    private lazy var regirstBtn: UIButton={
        let btn=UIButton()
        btn.setTitle("注 册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    private lazy var loginBtn: UIButton={
            let btn=UIButton()
            btn.setTitle("登 录", forState: UIControlState.Normal)
            btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
            return btn
            }()
}
